﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;

public partial class Moral_Teacher_ScoreInput_DayInput : ContainerBasePage
{
    protected static int EvalID
    {
        get
        {
            int _evalId;
            if (int.TryParse(HttpContext.Current.Request["id"], out _evalId))
                return _evalId;
            return -1;
        }
    }

    protected static int Attribute
    {
        get
        {
            int _attribute;
            if (int.TryParse(HttpContext.Current.Request["attribute"], out _attribute))
                return _attribute;
            return -1;
        }
    }

    protected static int EvalType
    {
        get
        {
            int _evalType;
            if (int.TryParse(HttpContext.Current.Request["evaltype"], out _evalType))
                return _evalType;
            return -1;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindWeek(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                      SchoolCommon.Sch_SchoolInfo.SchoolTerm);

            selWeek_IndexChanged(selWeek as object, new EventArgs());
        }
    }

    protected void selWeek_IndexChanged(object sender, EventArgs e)
    {
        int week = -1;
        if (!Int32.TryParse(selWeek.SelectedValue, out week))
            return;

        lbTitle.Text = "本学期评价记录";
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("IsEffect", "1");
        IList<ClsInfo> clsList = Container<IClsInfoLogic>.Instance.FindAll(ht);

        DateTime startDay = new DateTime();
        DateTime endDay = new DateTime();
        if (!SchoolCommon.GetWeekInTerm(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                    SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                    SchoolMark,
                                    week, out startDay, out endDay))
            return;

        IList<ClassEvalState> stateList =
            Container<IClassEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                                SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                                                SchoolMark, EvalID, startDay, endDay);
        CreateTable(clsList, stateList,
            SchoolCommon.Sch_SchoolInfo.SchoolYear,
            SchoolCommon.Sch_SchoolInfo.SchoolTerm, startDay);
    }

    /// <summary>
    /// 初始化月份选择下拉框
    /// </summary>
    /// <param name="schoolYear"></param>
    /// <param name="term"></param>
    protected void BindWeek(int schoolYear, int term)
    {
        selWeek.Items.Clear();

        int numberOfWeek = 0;
        if (!SchoolCommon.GetWeekOfTerm(schoolYear, term, SchoolMark, DateTime.Now, out numberOfWeek))
        {
            selWeek.Items.Add(new ListItem("-----", "-1"));
            return;
        }

        for (int i = numberOfWeek; i > 0; i--)
        {
            selWeek.Items.Add(new ListItem(String.Format("第{0}周", i), i.ToString()));
        }
    }

    private void CreateTable(IList<ClsInfo> classInfo, IList<ClassEvalState> stateInfo,int year, int term, DateTime day)
    {
        int width = 90;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hrow.Cells.Add(hcell);

        IList<DateTime> dateList = new List<DateTime>();
        ListSort<ClassEvalState> sort = new ListSort<ClassEvalState>(stateInfo, "StartTime", false);
        stateInfo = sort.Sort();

        for (int i = 1; i <= 7; i++)
        {
            DateTime dttemp = day.AddDays(i - 1);
            if(dttemp > DateTime.Now)
                break;
            dateList.Add(dttemp);

            hcell = new TableHeaderCell();
            hcell.Text = String.Format("周{0}({1})", i, dttemp.ToString("yy-MM-dd"));
            hcell.Style[HtmlTextWriterStyle.Width] = "80px";
            width += 80;
            hrow.Cells.Add(hcell);
        }
        tbResult.Rows.Add(hrow);

        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);

        // 填充表内容
        TableRow row = null;
        TableCell cell = null;
        foreach (ClsInfo info in classInfo)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = info.ClsName;
            row.Cells.Add(cell);
            int i = 0;
            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                foreach (ClassEvalState state in stateInfo)
                {
                    if (state.ClassID == info.RID && state.StartTime.Month == dt.Month && state.StartTime.Day == dt.Day)
                    {
                        // 构造超链html标签
                        HyperLink control = new HyperLink();
                        control.Text = "已录入";
                        control.Attributes.Add("onclick", "detailView();");
                        control.NavigateUrl = String.Format("../ScoreInput/ClassMonthInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&evalDate={3}&Type=Edit&Attribute={4}&evalID={5}",
                                                year, term,
                                                state.ClassID, state.StartTime.ToString("yyyy-MM-dd"), Attribute, EvalID);
                        cell.Controls.Add(control);
                        sign = true;
                        break;
                    }
                }
                if (!sign)
                {
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "未录入";
                    control.Attributes.Add("onclick", "detailView();");
                    control.NavigateUrl = String.Format("../ScoreInput/ClassMonthInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&evalDate={3}&Type=Edit&Attribute={4}&evalID={5}",
                                            year, term,
                                            info.RID, dt.ToString("yyyy-MM-dd"), Attribute, EvalID);
                    cell.Controls.Add(control);
                }
                cell.Attributes.Add("onmouseout", "mout(" + i + ");");
                cell.Attributes.Add("onmouseover", "mover(" + i + ");");
                row.Cells.Add(cell);
                i++;
            }

            tbResult.Rows.Add(row);
        }
    }
}
